package com.xinqi.modules.prepare.question.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.prepare.question.domain.PrepareQuestionFolderEntity;
import com.xinqi.modules.prepare.question.dto.req.PrepareQuestionFolderQueryDTO;
import com.xinqi.modules.prepare.question.dto.rsp.PrepareQuestionFolderResultDTO;
import com.xinqi.modules.prepare.question.manager.PrepareQuestionFolderManager;
import com.xinqi.modules.prepare.question.mapper.PrepareQuestionFolderMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 备课区试题夹管理层实现
 *
 * @author: system
 * @date: 2023/03/19
 */
@Service
@RequiredArgsConstructor
public class PrepareQuestionFolderManagerImpl extends BaseServiceImpl<PrepareQuestionFolderMapper, PrepareQuestionFolderEntity> implements PrepareQuestionFolderManager {

    /**
     * 自定义分页查询
     */
    @Override
    public Page<PrepareQuestionFolderResultDTO> findPage(Page<Object> page, PrepareQuestionFolderQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<PrepareQuestionFolderResultDTO> findList(PrepareQuestionFolderQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 备课区试题夹数据
     */
    @Override
    public PrepareQuestionFolderEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id删除 备课区试题夹数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    @Override
    public Boolean isExistName(Long prepareId, String name, Long oldId) {
        LambdaQueryWrapper<PrepareQuestionFolderEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrepareQuestionFolderEntity::getPrepareId, prepareId);
        wrapper.eq(PrepareQuestionFolderEntity::getName, name);
        wrapper.ne(oldId != null, PrepareQuestionFolderEntity::getId, oldId);
        return count(wrapper) > 0;
    }

}
